package binary_tree.leetcode.medium;

/**
 * @author bruin_du
 * @description 二叉搜索树与双向链表
 * @date 2022/5/28 10:44
 **/
public class Offer36_TreeToDoublyList {
    Node prev = null,head = null;
    public Node treeToDoublyList(Node root) {
        if(root == null) return null;
        recursion(root);
        prev.right = head;
        head.left = prev;
        return head;
    }

    private void recursion(Node root) {
        if(root == null) return;
        recursion(root.left);
        if(prev == null) head = root;
        else prev.right = root;
        root.left = prev;
        prev = root;
        recursion(root.right);
    }
}

class Node {
    public int val;
    public Node left;
    public Node right;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val,Node _left,Node _right) {
        val = _val;
        left = _left;
        right = _right;
    }
};
